package org.jeecg.modules.workgroup.service.impl;

import org.jeecg.modules.workgroup.entity.WorkGroup;
import org.jeecg.modules.workgroup.entity.GroupUser;
import org.jeecg.modules.workgroup.entity.GroupProc;
import org.jeecg.modules.workgroup.mapper.GroupUserMapper;
import org.jeecg.modules.workgroup.mapper.GroupProcMapper;
import org.jeecg.modules.workgroup.mapper.WorkGroupMapper;
import org.jeecg.modules.workgroup.service.IWorkGroupService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 班组表
 * @Author: jeecg-boot
 * @Date:   2020-07-24
 * @Version: V1.0
 */
@Service
public class WorkGroupServiceImpl extends ServiceImpl<WorkGroupMapper, WorkGroup> implements IWorkGroupService {

	@Autowired
	private WorkGroupMapper workGroupMapper;
	@Autowired
	private GroupUserMapper groupUserMapper;
	@Autowired
	private GroupProcMapper groupProcMapper;

	@Override
	@Transactional
	public void saveMain(WorkGroup workGroup, List<GroupUser> groupUserList,List<GroupProc> groupProcList) {
		workGroupMapper.insert(workGroup);
		if(groupUserList!=null && groupUserList.size()>0) {
			for(GroupUser entity:groupUserList) {
				//外键设置
				entity.setWorkGroupId(workGroup.getId());
				groupUserMapper.insert(entity);
			}
		}
		if(groupProcList!=null && groupProcList.size()>0) {
			for(GroupProc entity:groupProcList) {
				//外键设置
				entity.setWorkGroupId(workGroup.getId());
				groupProcMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(WorkGroup workGroup,List<GroupUser> groupUserList,List<GroupProc> groupProcList) {
		workGroupMapper.updateById(workGroup);

		//1.先删除子表数据
		groupUserMapper.deleteByMainId(workGroup.getId());
		groupProcMapper.deleteByMainId(workGroup.getId());

		//2.子表数据重新插入
		if(groupUserList!=null && groupUserList.size()>0) {
			for(GroupUser entity:groupUserList) {
				//外键设置
				entity.setWorkGroupId(workGroup.getId());
				groupUserMapper.insert(entity);
			}
		}
		if(groupProcList!=null && groupProcList.size()>0) {
			for(GroupProc entity:groupProcList) {
				//外键设置
				entity.setWorkGroupId(workGroup.getId());
				groupProcMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		groupUserMapper.deleteByMainId(id);
		groupProcMapper.deleteByMainId(id);
		workGroupMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			groupUserMapper.deleteByMainId(id.toString());
			groupProcMapper.deleteByMainId(id.toString());
			workGroupMapper.deleteById(id);
		}
	}

}
